<?php

/*	LibFox - Simple library for XML-based backends.
	Copyright (C) 2008 LinFox Serviços em Informática LTDA.

	This file is part of LibFox.

	LibFox is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	LibFox is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>. */

session_start();

header('Content-Type: text/xml; charset=utf-8');
header('Cache-control: private');
date_default_timezone_set('America/Recife');

require 'config.php';
require 'inc/db.php';
require 'inc/functions.php';
require 'inc/config.php';
require_once 'inc/changes.php';
require_once 'inc/config_dao.php';
require_once 'inc/netcontrol_ctl.php';
require_once 'inc/user_dao.php';

$config = config::get_instance();
require_once 'lang/'.$config->language.'.php';

$user_dao = new user_dao();
$user = $user_dao->auth();

if (!$action = get('action')) $action = 'show_all';
$result = '';

if ($user->verified) {
	$result .= '<currentuser>';
	$result .= '<username>' .$user->username. '</username>';
	$result .= '<verified>1</verified>';
	$result .= '</currentuser>';

	if ($mode = get('mode')) {
		$class = $mode.'_ctl';
		include_once('inc/'.$class.'.php');
		$controller = new $class();
		try {
			$result .= $controller->$action();
		} catch (Exception $e) {
			$error = $e->getMessage();
		}
	}

	$changes = changes::get_instance();
	if ($changes->changed) {
		$result .= '<dirty><dhcp>1</dhcp></dirty>';
		$netcontrol = new netcontrol_ctl();
		$netcontrol->refresh_iptables();
		$netcontrol->refresh_tc();
	}

} else {
	$result .= '<currentuser><verified>0</verified></currentuser>';
}

$result .= '<requests>';
foreach ($_POST as $key => $value) {
	 $result .= '<request><key>'.$key.'</key><value>'.$value.'</value></request>';
}
$result .= '</requests>';

echo '<?xml version="1.0" encoding="utf-8"?>
<'.setup::sitename.' xmlns="">
<mode>'.$mode.'</mode>
<action>'.$action.'</action>
'.$result.'
<error>'.$error.'</error>
</'.setup::sitename.'>';

?>